package com.sunstar.androidmedia.camera

import android.Manifest
import android.app.Activity
import android.content.pm.PackageManager
import android.graphics.ImageFormat
import android.util.Log
import androidx.camera.core.*
import androidx.camera.lifecycle.ProcessCameraProvider
import androidx.camera.view.PreviewView
import androidx.core.app.ActivityCompat
import androidx.core.content.ContextCompat
import androidx.lifecycle.LifecycleOwner

class CameraHelper(val ac: Activity, val preview: PreviewView) {

    private var cameraPermissionRequestCode = 101

    private var permissionArray = arrayOf(Manifest.permission.CAMERA)

    private var cameraSwitch = CameraSelector.LENS_FACING_BACK

    val imgAnalysis by lazy {
        ImageAnalysis.Builder().setBackpressureStrategy(ImageAnalysis.STRATEGY_KEEP_ONLY_LATEST)
            .build()
    }

    val cameraProviderFuture by lazy { //请求cameraProvider
        ProcessCameraProvider.getInstance(ac)
    }

    val executor by lazy {
        ContextCompat.getMainExecutor(ac)
    }

    var camera: Camera? = null


    fun cameraPreview() {
        if (hasPermission()) {
            startPreviewCamera()
        } else {
            requestPermission()
        }
    }

    fun startPreviewCamera() {
        cameraProviderFuture.addListener(Runnable {
            val cameraProvider = cameraProviderFuture.get()
            bindView(cameraProvider)
        }, executor)
    }

    private fun bindView(cameraProvider: ProcessCameraProvider) {
        val helperPreview = Preview.Builder().build()
        val cameraSelector = CameraSelector.Builder().requireLensFacing(cameraSwitch).build()
        helperPreview.setSurfaceProvider(preview.surfaceProvider)
        imgAnalysis.setAnalyzer(executor,
            {
                if(ImageFormat.YUV_420_888 !=it.format){
                    Log.e("这里是数据包","这里可能不太对")
                }
                it.close()
            })
        camera = cameraProvider.bindToLifecycle(
            ac as LifecycleOwner,
            cameraSelector,
            imgAnalysis,
            helperPreview
        )
    }


    private fun requestPermission() {
        ActivityCompat.requestPermissions(ac, permissionArray, cameraPermissionRequestCode)
    }

    private fun hasPermission(): Boolean {
        val permissionStatus = ActivityCompat.checkSelfPermission(ac, Manifest.permission.CAMERA)
        return PackageManager.PERMISSION_GRANTED == permissionStatus
    }

    fun permissionCheck(requestCode: Int, permissions: Array<out String>, grantResults: IntArray) {
        var permissionIndex = -1;
        var hasPermission: Boolean = false
        if (requestCode == cameraPermissionRequestCode) {
            permissionIndex = permissions.indexOf(Manifest.permission.CAMERA)
        }
        if (permissionIndex != -1) {
            hasPermission = PackageManager.PERMISSION_GRANTED == grantResults[permissionIndex]
        }
        if (hasPermission) {
            startPreviewCamera()
        }
    }
}